package com.lyn.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import com.lyn.base.interfaces.ClickItemListener;
import com.lyn.base.interfaces.ClickLongItemListener;
import com.lyn.databinding.AdapterEmptyItemBinding;
import java.util.ArrayList;
import java.util.List;

/**
 * recyclerView 适配器
 * @author longyn 2023/10/10
 * @version 1.0.0
 */
public abstract class BaseRecyclerAdapter<V extends ViewBinding, T>
        extends RecyclerView.Adapter<BaseViewHolder> {

    public Context context;

    public Context getContext(){
        return context;
    }

    private final List<T> list = new ArrayList<>();

    private EmptyListener emptyListener;

    private ClickItemListener clickItemListener;

    private ClickLongItemListener clickLongItemListener;

    public BaseRecyclerAdapter(@NonNull Context context) {
        this.context = context;
    }

    public void setEmptyListener(@NonNull EmptyListener emptyListener) {
        this.emptyListener = emptyListener;
    }

    public void setClickItemListener(@NonNull ClickItemListener clickItemListener) {
        this.clickItemListener = clickItemListener;
    }

    public void setClickLongItemListener(@NonNull ClickLongItemListener clickLongItemListener) {
        this.clickLongItemListener = clickLongItemListener;
    }

    public List<T> getList() {
        return list;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addItem(@NonNull T t, boolean clear) {
        if (clear) {
            this.list.clear();
        }
        this.list.add(t);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addAllItem(@NonNull List<T> tList, boolean clear) {
        if (clear) {
            this.list.clear();
        }
        this.list.addAll(tList);
        notifyDataSetChanged();
    }

    public T getItem(int position) {
        if (this.list.isEmpty()) {
            return null;
        }
        return this.list.get(position);
    }


    public abstract V getViewBinding(ViewGroup parent, int viewType);

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == -1) {
            LayoutInflater inflater = LayoutInflater.from(context);
            AdapterEmptyItemBinding emptyItemBinding = AdapterEmptyItemBinding.inflate(inflater,parent,false);
            return new BaseEmptyViewHolder(emptyItemBinding);
        }
        return new BaseViewHolder(getViewBinding(parent, viewType));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        View root = holder.binding.getRoot();
        root.setOnClickListener(v -> {
            if (clickItemListener!=null){
                clickItemListener.onClickItem(holder, position);
            }
        });

        root.setOnLongClickListener(v -> {
            if (clickLongItemListener!=null){
                clickLongItemListener.onClickLongItem(holder, position);
            }
            return true;
        });
    }

    @Override
    public int getItemCount() {
        if (this.list.isEmpty()) {
            if (emptyListener!=null){
                emptyListener.onEmpty();
            }
            return 1;
        }
        if (emptyListener!=null){
            emptyListener.onNotEmpty();
        }
        return this.list.size();
    }

   public interface EmptyListener {

        /**
         * 数据空
         */
        void onEmpty();

        /**
         * 数据不为空
         */
        void onNotEmpty();
    }
}
